package nl.windgazer.maven.plugins.jsmeter;


import java.io.File;
import java.util.Locale;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

/** 
 * Run JSMeter on the current webapp. This mojo contains a version of
 * JSMeter and wraps around it to be able to run it using Maven 2.<br />
 * Developers have access to all the 'options' that JSMeter has and
 * they can be overridden using mavens' plugin configuration.<br />
 * <br />
 * See the <code>JSMeterGenerator</code> for configuration options.
 *
 * @see JSMeterGenerator
 * @author mreuring
 * @goal jsmeter
 */
public class JSMeterMojo extends AbstractMojo {
	/**
	 * MAVEN INTERNAL
	 * 
	 * @parameter expression="${basedir}"
	 */
	private File basedir;
	
	/**
     * The temporary directory where JSMeter toolkit is stored while running.
     * This is not a JSMeter property, and won't be found in the JSMeter documentation.
     *
     * @parameter expression="${project.build.directory}/toolkit"
     */
	private File tempDir;
	
	/**
     * The target directory where you want the generated doc to end up. This
     * property represents: -d=&lt;PATH> or --directory=&lt;PATH>.
     *
     * @parameter expression="${project.build.directory}/jsmeter"
     */
	private File directory;
	
	/**
	 * The level of recursion. Default is 10.
	 * 
	 * @parameter expression="10"
	 */
	private int recurseLevel;
	
	/**
	 * The level of cyclomatic complexity that must not be exceeded. Default is 10.
	 * 
	 * @parameter expression="10"
	 */
	private int complexityLimit;
	
	/**
	 * The directory where we may find the source-files we're making jsmeters for.
	 * This property represents: &lt;SRC_DIR>
	 * 
	 * @parameter expression="${basedir}/src/main/webapp"
	 */
	private File srcDir; //TODO: Find the expression for the current project-dir!

	/**
	 * Execute the mojo, this is what mvn calls to start this mojo.
	 * @throws MojoFailureException 
	 */
	public void execute() throws MojoExecutionException, MojoFailureException {
		System.setProperty("basedir", basedir.getAbsolutePath());
		Locale locale = Locale.getDefault();
		try {
			JSMeterGenerator gen = new JSMeterGenerator(tempDir, directory, srcDir, complexityLimit, recurseLevel, getLog());
			gen.generateReport(locale);
		} catch (JSMeterException e) {
			throw new MojoExecutionException(e.getLocalizedMessage());
		}
	}

}
